import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:school_assistant_m/api/system/user_api.dart';
import 'package:school_assistant_m/app/modules/mine/controllers/settings_controller.dart';
import 'package:school_assistant_m/store/login_store.dart';
import 'package:school_assistant_m/utils/my_toast.dart';

class EditPasswordController extends GetxController {
  UserApi userApi = Get.find<UserApi>();
  SettingsController settingsController = Get.find<SettingsController>();
  GlobalKey<FormState> editPasswordKey = GlobalKey<FormState>(); // 修改密码表单key
  TextEditingController username = TextEditingController(); // 用户名
  TextEditingController oldPassword = TextEditingController(); // 旧密码
  TextEditingController newPassword = TextEditingController(); // 新密码
  TextEditingController newPasswordConfirm = TextEditingController(); // 新密码确认
  bool isShowOldPassword = false; // 是否展示旧密码
  bool isShowNewPassword = false; // 是否展示新密码
  bool isShowConfirmPassword = false; // 是否展示确认密码
  LoginStore store = LoginStore();
  String _oldPassword = ''; // 缓存的原密码
  Map editPasswordForm = {
    'username': '',
    'oldPassword': '',
    'newPassword': '',
  };

  @override
  void onInit() async {
    username.text = await store.getUsername();
    _oldPassword = await store.getPassword();
    super.onInit();
  }

  /// 更新是否展示原密码明文
  void changeIsShowOldPassword() {
    isShowOldPassword = !isShowOldPassword;
    update(['edit_password']);
  }

  /// 更新是否展示新密码明文
  void changeIsShowNewPassword() {
    isShowNewPassword = !isShowNewPassword;
    update(['edit_password']);
  }

  /// 更新是否展示确认密码明文
  void changeIsShowConfirmPassword() {
    isShowConfirmPassword = !isShowConfirmPassword;
    update(['edit_password']);
  }

  /// 校验原密码
  String verifyOldPassword(oldPassword) {
    String result = '';
    // 校验是否为空
    if (!oldPassword.trim().isNotEmpty) {
      return result = '原密码不能为空！';
    }

    // 校验原密码是否正确
    if (oldPassword != _oldPassword) {
      return result = '原密码不正确！';
    }
    return result;
  }

  /// 校验旧密码
  String verifyNewPassword(newPassword) {
    String result = '';
    // 校验是否为空
    if (!newPassword.trim().isNotEmpty) {
      return result = '新密码不能为空！';
    }

    return result;
  }

  /// 校验确认密码
  String verifyConfirmPassword(confirmPassword) {
    String result = '';
    // 校验是否为空
    if (!confirmPassword.trim().isNotEmpty) {
      return result = '确认密码不能为空！';
    }

    // 校验确认密码是否和新密码一致
    if (confirmPassword != newPassword.text) {
      return result = '两次输入密码不一致！';
    }

    return result;
  }

  /// 设置提交修改密码的表单数据
  void setEditPasswordForm() {
    editPasswordForm['username'] = username.text;
    editPasswordForm['oldPassword'] = oldPassword.text;
    editPasswordForm['newPassword'] = newPassword.text;
  }

  /// 执行修改密码
  void editPassword() {
    var form = editPasswordKey.currentState;
    if (form!.validate()) {
      setEditPasswordForm();
      print('------------------------我是editPasswordForm---------------------------');
      print(editPasswordForm);
      userApi.updatePwd(editPasswordForm).then((res) {
        if (res.body['code'] == 200) {
          MyToast.success('密码修改成功！');
          settingsController.logout();
        } else {
          MyToast.danger(res.body['msg']);
        }
      });
    }
  }
}
